package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;

import com.limao.DBDataClass.UserOffLineDataClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;

public class UserOfflineProvider {

	private static class DB_user_offline_ProviderContainer {
		private static UserOfflineProvider instance = new UserOfflineProvider();
	}

	public static UserOfflineProvider getInstance() {
		return DB_user_offline_ProviderContainer.instance;
	}

	// 定义系统表的name与操作语句
	private static final String table = "cd_user_offline";
	private static final String cd_user_offline_LOAD = "SELECT * FROM " + table
			+ " WHERE userid=? and platform=?";
	
	private static final String cd_user_offline_LOAD2 = "SELECT logintime FROM " + table
			+ " WHERE userid=? order by logintime desc ";
	// 初始化
	private UserOfflineProvider() {}

	// cd_user_offline
	public UserOffLineDataClass load_User_Offline(int userid, int platform) {
		UserOffLineDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(cd_user_offline_LOAD);
			pstmt.setInt(1, userid);
			pstmt.setInt(2, platform);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				tmp = new UserOffLineDataClass();
				tmp.userid = userid + "";
				tmp.platform = platform + "";
				tmp.token = rs.getString("token") ;
				tmp.onlined = rs.getInt("onlined") + "";
				tmp.logintime = rs.getLong("logintime") + "";
				tmp.loginouttime = rs.getLong("loginouttime") + "";
			}
		} catch (Exception e) {
			tmp = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}
	
	// cd_user_offline
	public boolean load_User_Offline2(int userid) {
		boolean tmp = false;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(cd_user_offline_LOAD2);
			pstmt.setInt(1, userid);
			rs = pstmt.executeQuery();
			if (!rs.next()) {
				tmp=true;
			}
		} catch (Exception e) {
			tmp = false;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}
	
	
		
	
	//App用户打开及关闭页面操作时间记录,onlined(1:打开 ,0:关闭)
	public void recordAppUsetime(int userid, int type, int onlined) {
		Connection con = null;
		PreparedStatement pstmt = null;
		String sql = "";
		try {
			con = DbConnectionManager.getConnection();
			if(onlined == 1){
				sql = "replace into cd_user_usetime(userid,type,starttime) values(?,?,?)";
				pstmt = con.prepareStatement(sql);
				pstmt.setInt(1, userid);
				pstmt.setInt(2, type);
				pstmt.setLong(3, CacheClass.getTime());
				pstmt.executeUpdate();
			}else{
				sql = "update cd_user_usetime set endtime=? where userid=? and type=? ";
				pstmt = con.prepareStatement(sql);
				pstmt.setLong(1, CacheClass.getTime());
				pstmt.setInt(2, userid);
				pstmt.setInt(3, type);
				pstmt.executeUpdate();
				
				sql = "insert into cd_user_userecord(userid,type,starttime,endtime) "
						+ "select userid,type,starttime,endtime from cd_user_usetime u "
						+ "where u.userid=? and u.type=? ";
				DbConnectionManager.fastcloseStmt(pstmt);
				pstmt = con.prepareStatement(sql);
				pstmt.setInt(1, userid);
				pstmt.setInt(2, type);
				pstmt.executeUpdate();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(null, pstmt, con);
		}
	}

	//onlined(1:登入 ,0:登出)
	public UserOffLineDataClass updateUserOffline(int id, int platform, int onlined,String token) {
		UserOffLineDataClass useroff = load_User_Offline(id, platform);
		if(useroff == null){
			return insertUserOffline(id, platform, onlined,token);
		}else{
			return updateUserOffline(id, platform, onlined, CacheClass.getLongValue(useroff.logintime),token);
		}
	}
	
	//新增用户登录登出记录 onlined(1:登入 ,0:登出)
	public UserOffLineDataClass insertUserOffline(int id, int platform, int onlined,String token) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		UserOffLineDataClass useroff = null;
		try {
			long time = CacheClass.getTime();
			useroff = new UserOffLineDataClass();
			String sql = "insert  into cd_user_offline "
					 		+ " (userid,platform,token,onlined,logintime,loginouttime) VALUES (?,?,?,?,?,?) ";;

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, id);
			pstmt.setInt(2, platform);
			
			pstmt.setString(3, token);
			
			
			pstmt.setInt(4, onlined);
			if (onlined == 1) // 登入
			{
				pstmt.setLong(5, time);
				pstmt.setNull(6, Types.VARCHAR);
				
				useroff.logintime = time + "";
				useroff.loginouttime = "0";
			} else // 登出
			{
				pstmt.setLong(5, 0L);
				pstmt.setLong(6, time);
				
				useroff.logintime = "0";
				useroff.loginouttime = time + "";
			}

			pstmt.executeUpdate();
			
			useroff.userid = id + "";
			useroff.platform = platform + "";
			useroff.token = token;
			useroff.onlined = onlined + "";
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return useroff;
	}
	
	//增加登陆记录
	public void insertUserLogLogin(int id) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("insert into cd_log_login (userid,time) VALUES (?,?) ");
			pstmt.setInt(1, id);
			pstmt.setLong(2, CacheClass.getTime());
			pstmt.execute();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
	}	
	
	//修改用户登录登出记录 onlined(1:登入 ,0:登出)
	public UserOffLineDataClass updateUserOffline(int id, int platform, int onlined, long logintime,String token) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		UserOffLineDataClass useroff = null;
		try {
			long time = CacheClass.getTime();
			useroff = new UserOffLineDataClass();

			if (onlined == 1) // 登入
			{
				String sql = "update cd_user_offline o set o.onlined=?,o.logintime=?,o.loginouttime=?,o.token=? "
						+"where o.userid=? and o.platform=?";

				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement(sql);
				pstmt.setInt(1, onlined);
				pstmt.setLong(2, time);
				pstmt.setNull(3, Types.VARCHAR);
				pstmt.setString(4, token);
				pstmt.setInt(5, id);
				pstmt.setInt(6, platform);
				pstmt.executeUpdate();
				
				useroff.userid = id + "";
				useroff.platform = platform + "";
				useroff.onlined = onlined + "";
				useroff.logintime = time + "";
				useroff.loginouttime = "0";
				useroff.token = token;
				
			} else // 登出
			{
				String sql = "update cd_user_offline o set o.onlined=?,o.logintime=?,o.loginouttime=? "
						+"where o.userid=? and o.platform=?";

				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement(sql);
				pstmt.setInt(1, onlined);
				pstmt.setLong(2, logintime);
				pstmt.setLong(3, time);
				pstmt.setInt(4, id);
				pstmt.setInt(5, platform);
				pstmt.executeUpdate();
				
				useroff.userid = id + "";
				useroff.platform = platform + "";
				useroff.onlined = onlined + "";
				useroff.logintime = logintime+"";
				useroff.loginouttime = time + "";
				
			}

	
			

			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return useroff;
	}
}
